import time

import allure
from selenium.webdriver.common.by import By

from .base import Base


class Check(Base):  # 查询
    __SID = (By.XPATH, "//div[@class='filter-container']/div[1]/input")         # ID
    __NUMBER = (By.XPATH, "//div[@class='filter-container']/div[2]/input")      # 编号
    __NAME = (By.XPATH, "//div[@class='filter-container']/div[3]/input")        # 名字
    __CHECK = (By.XPATH, "//div[@class='filter-container']/button[1]")          # 查询按钮
    # 查询第一个按钮   查询第一个编号     查询的第一个ID    查询的第一个名字
    __GET_MESSAGE__BUTTON = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:nth-child(1) > div")
    __GET_MESSAGE__NUMBER = (By.CSS_SELECTOR, "tbody > tr:nth-child(2) form > div:nth-child(1) > div > span ")
    __GET_MESSAGE__SID = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:nth-child(2) > div")
    __GET_MESSAGE__NAME = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:nth-child(3) > div")
    __GET_NEW_PRODUCT = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:nth-child(9) span")     # 是否新品
    __GET_HOT_SALE = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:nth-child(10) span")     # 是否热卖
    __GET_FOR_SALE = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:nth-child(11) span")     # 是否在售
    __CHECK_TITLE = (By.CSS_SELECTOR, "div[class='el-table el-table--fit el-table--border el-table--enable-row-hover el-table--enable-row-transition el-table--mini']")    # 列表标题栏
    __CHECK_NO_DATA = (By.CSS_SELECTOR, "span.el-table__empty-text")    # 商品列表无数据
    __EDIT_PAGE = (By.CSS_SELECTOR, "tbody > tr:nth-child(1) > td:last-child button:nth-child(1)")  # 编辑页

    def check(self, sid=None, number=None, name=None, img_name=None):    # 查询信息
        if not (sid or number or name):     # 无数据时,会获取列表的提示信息
            self.find_click(self.__CHECK)
            return Check(self.driver)
        self.wait(self.__CHECK, time_out=30)
        with allure.step("查询"):
            if sid:     # ID输入框输入sid
                self.find_key(self.__SID, sid)
            if number:  # 编号输入框输入number
                self.find_key(self.__NUMBER, number)
            if name:    # 名字输入框输入name
                self.find_key(self.__NAME, name)
            self.find_click(self.__CHECK)
            if img_name:    # 截图
                self.implicitly_wait(0.3)
                if self.find(self.__CHECK_TITLE):       # 查询有信息时
                    self.images_log(img_name, "查询信息")
                if self.find(self.__CHECK_NO_DATA):     # 查询无信息时
                    self.images_log(img_name, "查询信息")
                self.implicitly_wait(10)
        return Check(self.driver)

    def go_delete(self):
        from .delete import Delete
        return Delete(self.driver)

    def get_message(self, number=None, sid=None, name=None, new_product=None, hot_sale=None, for_sale=None):  # 查询,注意查询的都为第一个
        if not self.estimate(number, sid, name, new_product, hot_sale, for_sale):    # 只能获取一个值,因为return只能返回一个值
            return "只能传入一个值"
        self.implicitly_wait(0.3)   # 隐式等待修改
        if self.find(self.__CHECK_NO_DATA):     # 如果查询无数据时,直接返回列表提示信息
            data = self.find_text(self.__CHECK_NO_DATA)
            return data
        self.implicitly_wait(10)
        if sid:     # 获取ID
            sid = self.find_text(self.__GET_MESSAGE__SID)
            return sid
        if number:  # 获取编号
            self.find_click(self.__GET_MESSAGE__BUTTON)
            number = self.find_text(self.__GET_MESSAGE__NUMBER)
            return number
        if name:    # 获取名字
            name = self.find_text(self.__GET_MESSAGE__NAME)
            return name
        if new_product:     # 获取是否新品
            new_product = self.find_text(self.__GET_NEW_PRODUCT)
            return new_product
        if hot_sale:        # 获取是否热卖
            hot_sale = self.find_text(self.__GET_HOT_SALE)
            return hot_sale
        if for_sale:        # 获取是否在售
            for_sale = self.find_text(self.__GET_FOR_SALE)
            return for_sale

    def estimate(self, *es):    # 判定*es是否一个值
        num = 0
        for i in es:
            if i:
                num += 1
        if num == 1:
            return True
        return False

    def goto_edit_page(self):   # 商品编辑页
        from .edit_page import EditPage
        with allure.step("去商品编辑页"):
            self.find_click(self.__EDIT_PAGE)
        return EditPage(self.driver)
